package cn.iocoder.yudao.module.student.controller.app.collectJob;

import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.member.api.company.dto.CompanyRespDTO;
import cn.iocoder.yudao.module.member.api.job.JobApi;
import cn.iocoder.yudao.module.member.api.job.dto.JobRespDTO;
import cn.iocoder.yudao.module.student.controller.app.collectCompany.vo.AppCollectCompanyPageReqVO;
import cn.iocoder.yudao.module.student.controller.app.collectCompany.vo.AppCollectCompanyUpdateReqVO;
import cn.iocoder.yudao.module.student.dal.dataobject.collectCompany.CollectCompanyDO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.student.controller.app.collectJob.vo.*;
import cn.iocoder.yudao.module.student.dal.dataobject.collectJob.CollectJobDO;
import cn.iocoder.yudao.module.student.convert.collectJob.CollectJobConvert;
import cn.iocoder.yudao.module.student.service.collectJob.CollectJobService;

@Api(tags = "用户 APP - 收藏职位")
@RestController
@RequestMapping("/student/collect-job")
@Validated
public class AppCollectJobController {

    @Resource
    private CollectJobService collectJobService;
    @Resource
    private JobApi jobApi;
    @PostMapping("/create")
    @ApiOperation("创建收藏职位")

    public CommonResult<Long> createCollectJob(@Valid @RequestBody AppCollectJobCreateReqVO createReqVO) {
        return success(collectJobService.createCollectJob(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新收藏职位")

    public CommonResult<Boolean> updateCollectJob(@Valid @RequestBody AppCollectJobUpdateReqVO updateReqVO) {
        collectJobService.updateCollectJob(updateReqVO);
        return success(true);
    }
    @PostMapping("/createOrUpdateCollectJob")
    @ApiOperation("收藏/取消收藏企业")
    public CommonResult<Boolean> createOrUpdateCollectJpb( @RequestBody AppCollectJobUpdateReqVO updateReqVO) {
        collectJobService.createOrUpdateCollectJob(updateReqVO);
        return success(true);
    }
    @DeleteMapping("/delete")
    @ApiOperation("删除收藏职位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)

    public CommonResult<Boolean> deleteCollectJob(@RequestParam("id") Long id) {
        collectJobService.deleteCollectJob(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得收藏职位")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)

    public CommonResult<AppCollectJobRespVO> getCollectJob(@RequestParam("id") Long id) {
        CollectJobDO collectJob = collectJobService.getCollectJob(id);
        if(collectJob==null){
            return  success(null);
        }
        JobRespDTO jobRespDTO= jobApi.getJobAll(collectJob.getJobId());
        AppCollectJobRespVO appCollectJobRespVO=CollectJobConvert.INSTANCE.convert(collectJob);
        appCollectJobRespVO.setJobRespDTO(jobRespDTO);
        return success(appCollectJobRespVO);
    }

    @GetMapping("/list")
    @ApiOperation("获得收藏职位列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)

    public CommonResult<List<AppCollectJobRespVO>> getCollectJobList(@RequestParam("ids") Collection<Long> ids) {
        List<CollectJobDO> list = collectJobService.getCollectJobList(ids);
        return success(CollectJobConvert.INSTANCE.convertList(list));
    }

    @PostMapping("/page")
    @ApiOperation("获得收藏职位分页")
    public CommonResult<PageResult<JobRespDTO>> getCollectJobPage(@RequestBody  AppCollectJobPageReqVO pageVO) {
        pageVO.setStudentId(SecurityFrameworkUtils.getLoginUserId());
        PageResult<CollectJobDO> pageResult = collectJobService.getCollectJobPage(pageVO);
        List<CollectJobDO> collectJobDOS=pageResult.getList();
        List<JobRespDTO> jobRespDTOS=new ArrayList<>();
        for (CollectJobDO collectJobDO: collectJobDOS) {
            JobRespDTO jobRespDTO=jobApi.getJob1(collectJobDO.getJobId());
            if(jobRespDTO==null){
                continue;
            }
            jobRespDTO.setCollectTime(collectJobDO.getCollectTime());
            jobRespDTOS.add(jobRespDTO);
        }

        PageResult<JobRespDTO> jobRespDTOPageResult=new PageResult<>();
        jobRespDTOPageResult.setTotal(pageResult.getTotal());
        jobRespDTOPageResult.setList(jobRespDTOS);
        return success(jobRespDTOPageResult);
    }

    @PostMapping("/isCollect")
    @ApiOperation("获得收藏企业分页")
    public CommonResult<Boolean> isCollect(@RequestBody AppCollectJobPageReqVO pageVO) {
        return success(collectJobService.isCollect(pageVO));
    }


    @GetMapping("/export-excel")
    @ApiOperation("导出收藏职位 Excel")
    @OperateLog(type = EXPORT)
    public void exportCollectJobExcel(@Valid AppCollectJobExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<CollectJobDO> list = collectJobService.getCollectJobList(exportReqVO);
        // 导出 Excel
        List<AppCollectJobExcelVO> datas = CollectJobConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "收藏职位.xls", "数据", AppCollectJobExcelVO.class, datas);
    }

}
